<?php
/**
*
* @package ppkBB3cker
* @version $Id: install_update.php 1.000 2010-01-26 15:25:00 PPK $
* @copyright (c) 2010 PPK
* http://www.ppkbb3cker.ru, http://protoneutron.narod.ru
*
*/

error_reporting(0);

define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './../';
$phpEx = substr(strrchr(__FILE__, '.'), 1);

require($phpbb_root_path.'config.'.$phpEx);

if($dbms!='mysql')
{
	err('Only mysql supported');
}

$c=@mysql_connect($dbhost, $dbuser, $dbpasswd);
if(!$c)
{
	err('Error connecting database: '.mysql_error());
}

$s=@mysql_select_db($dbname, $c);
if(!$s)
{
	err('Error selecting database: '.mysql_error());
}

$result=my_sql_query('SELECT VERSION() AS version');
$mv=mysql_fetch_row($result);

$m_version=$mv[0];

if(version_compare($m_version, '4.1.3', '>='))
{
	$mysql_dbms=41;
}
else
{
	$mysql_dbms=40;
}

my_sql_query("SET NAMES 'utf8'");

if($mysql_dbms==41)
{

}
else
{

}

$result=my_sql_query("INSERT INTO `{$table_prefix}tracker_config` (`config_name`, `config_value`) VALUES ('ppkbb_tcallow_unregtorr', '0')");
$result=my_sql_query("INSERT INTO `{$table_prefix}tracker_config` (`config_name`, `config_value`) VALUES ('ppkbb_gtcallow_unregtorr', '0')");

$result=my_sql_query("ALTER TABLE `{$table_prefix}tracker_torrents` ADD `unreg` TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '0'");

$result=my_sql_query("INSERT INTO `{$table_prefix}tracker_config` (`config_name`, `config_value`) VALUES ('ppkbb_gtcunregtorr_sessid', '')");

$result=my_sql_query("ALTER TABLE `{$table_prefix}tracker_guests` ADD `unreg` TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '0'");

$result=my_sql_query("ALTER TABLE `{$table_prefix}tracker_guests` ADD INDEX ( `unreg` )");

$result=my_sql_query("INSERT INTO `{$table_prefix}tracker_config` (`config_name`, `config_value`) VALUES ('ppkbb_trclear_unregtorr', '0')");

$result=my_sql_query("ALTER TABLE `{$table_prefix}tracker_torrents` ADD INDEX ( `forb` )");

$result=my_sql_query("ALTER TABLE `{$table_prefix}attachments` ADD `i_poster` TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '0', ADD INDEX ( `i_poster` )");

$result=my_sql_query("UPDATE `{$table_prefix}attachments` SET i_poster='1' WHERE extension IN('gif', 'jpeg', 'jpg', 'png', 'tga', 'tif', 'tiff') AND real_filename LIKE '%poster%'");

$result=my_sql_query("DELETE FROM `{$table_prefix}tracker_config` WHERE `{$table_prefix}tracker_config`.`config_name` = 'ppkbb_num_torrents' LIMIT 1");

$result=my_sql_query("DELETE FROM `{$table_prefix}tracker_config` WHERE `{$table_prefix}tracker_config`.`config_name` = 'ppkbb_num_comments' LIMIT 1");

$result=my_sql_query("INSERT INTO `{$table_prefix}config` (`config_name`, `config_value`, `is_dynamic`) VALUES ('num_torrents', '0', '1')");

$result=my_sql_query("INSERT INTO `{$table_prefix}config` (`config_name`, `config_value`, `is_dynamic`) VALUES ('num_comments', '0', '1')");

$result=my_sql_query("ALTER TABLE `{$table_prefix}tracker_af` ADD `i_poster` TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '0',
 ADD INDEX ( `i_poster` )");

$result=my_sql_query("UPDATE `{$table_prefix}tracker_af` SET i_poster='1' WHERE field_name LIKE 'poster%' OR field_name LIKE 'thumb\_poster%'");

$result=my_sql_query("UPDATE `{$table_prefix}tracker_af` SET i_poster='2' WHERE field_name LIKE 'screenshots%' OR field_name LIKE 'thumb\_screenshots%'");

$result=my_sql_query("ALTER TABLE `{$table_prefix}tracker_rannounces` ADD INDEX ( `torrent` )");

$result=my_sql_query("ALTER TABLE `{$table_prefix}tracker_torrents` ADD `lastseed` INT( 11 ) UNSIGNED NOT NULL DEFAULT '0',
 ADD `lastleech` INT( 11 ) UNSIGNED NOT NULL DEFAULT '0'");

$result=my_sql_query("ALTER TABLE `{$table_prefix}tracker_torrents` ADD `forum_id` MEDIUMINT( 8 ) UNSIGNED NOT NULL DEFAULT '0',
 ADD INDEX ( `forum_id` )");

$sql="SELECT t.forum_id, tr.id FROM `{$table_prefix}tracker_torrents` tr, `{$table_prefix}topics` t WHERE t.topic_id=tr.topic_id";
$result=my_sql_query($sql);
while($row=mysql_fetch_array($result))
{
	my_sql_query("UPDATE `{$table_prefix}tracker_torrents` SET forum_id='{$row['forum_id']}' WHERE id='{$row['id']}'", true);
}
mysql_free_result($result);

$result=my_sql_query("UPDATE `{$table_prefix}attachments` SET i_poster='2' WHERE extension IN('gif', 'jpeg', 'jpg', 'png', 'tga', 'tif', 'tiff') AND real_filename NOT LIKE '%poster%'");

$result=my_sql_query("ALTER TABLE `{$table_prefix}attachments` DROP INDEX `extension`");

$result=my_sql_query("ALTER TABLE `{$table_prefix}tracker_torrents` ADD `lastseedreq` INT( 11 ) UNSIGNED NOT NULL DEFAULT '0'");

$result=my_sql_query("ALTER TABLE `{$table_prefix}users` ADD `user_last_seedreq` INT( 11 ) UNSIGNED NOT NULL DEFAULT '0'");

$result=my_sql_query("DELETE FROM `{$table_prefix}tracker_config` WHERE `config_name` IN
('ppkbb_chat_last_cleanup',
'ppkbb_tracker_last_cleanup',
'ppkbb_gtracker_last_cleanup',
'ppkbb_cron_last_cleanup',
'ppkbb_newversion_lastcheck')");

$result=my_sql_query("INSERT INTO `{$table_prefix}config`(config_name, config_value, is_dynamic) VALUES ('ppkbb_chat_last_cleanup', 0, 1)");
$result=my_sql_query("INSERT INTO `{$table_prefix}config`(config_name, config_value, is_dynamic) VALUES ('ppkbb_tracker_last_cleanup', 0, 1)");
$result=my_sql_query("INSERT INTO `{$table_prefix}config`(config_name, config_value, is_dynamic) VALUES ('ppkbb_gtracker_last_cleanup', 0, 1)");
$result=my_sql_query("INSERT INTO `{$table_prefix}config`(config_name, config_value, is_dynamic) VALUES ('ppkbb_cron_last_cleanup', 0, 1)");
$result=my_sql_query("INSERT INTO `{$table_prefix}config`(config_name, config_value, is_dynamic) VALUES ('ppkbb_newversion_lastcheck', 0, 1)");

$result=my_sql_query("INSERT INTO `{$table_prefix}config` (`config_name`, `config_value`, `is_dynamic`) VALUES ('ppkbb_total_peers_size', '', 1)");
$result=my_sql_query("INSERT INTO `{$table_prefix}config` (`config_name`, `config_value`, `is_dynamic`) VALUES ('ppkbb_total_up_down', '', 1)");
$result=my_sql_query("INSERT INTO `{$table_prefix}config` (`config_name`, `config_value`, `is_dynamic`) VALUES ('ppkbb_total_sup_sdown', '', 1)");
$result=my_sql_query("INSERT INTO `{$table_prefix}config` (`config_name`, `config_value`, `is_dynamic`) VALUES ('ppkbb_total_seed_leech', '', 1)");
$result=my_sql_query("INSERT INTO `{$table_prefix}config` (`config_name`, `config_value`, `is_dynamic`) VALUES ('ppkbb_total_tdown_tup', '', 1)");
$result=my_sql_query("INSERT INTO `{$table_prefix}config` (`config_name`, `config_value`, `is_dynamic`) VALUES ('ppkbb_total_udown_uup', '', 1)");
$result=my_sql_query("INSERT INTO `{$table_prefix}config` (`config_name`, `config_value`, `is_dynamic`) VALUES ('ppkbb_total_speedup_speeddown', '', 1)");

$result=my_sql_query("INSERT INTO `{$table_prefix}config` (`config_name`, `config_value`, `is_dynamic`) VALUES ('ppkbb_last_stattime', '0', '1')");

$result=my_sql_query("ALTER TABLE  `{$table_prefix}tracker_torrents` ADD  `lastcleanup` INT( 11 ) UNSIGNED NOT NULL DEFAULT  '0'");

$result=my_sql_query("ALTER TABLE  `{$table_prefix}tracker_torrents`
ADD `rem_seeders` int(10) unsigned NOT NULL DEFAULT '0',
ADD `rem_leechers` int(10) unsigned NOT NULL DEFAULT '0',
ADD `rem_times_completed` int(10) unsigned NOT NULL DEFAULT '0'");

$result=my_sql_query("ALTER TABLE  `{$table_prefix}tracker_torrents` ADD  `lastremote` INT( 11 ) UNSIGNED NOT NULL DEFAULT  '0'");

$result=my_sql_query("ALTER TABLE `{$table_prefix}tracker_rannounces`
  DROP `peers`");

$result=my_sql_query("ALTER TABLE  `{$table_prefix}attachments` ADD  `i_external` TINYINT UNSIGNED NOT NULL DEFAULT  '0'");

$result=my_sql_query("DELETE FROM `{$table_prefix}tracker_config` WHERE `{$table_prefix}tracker_config`.`config_name` = 'ppkbb_postscr_src'");

$result=my_sql_query("DELETE FROM `{$table_prefix}tracker_config` WHERE `{$table_prefix}tracker_config`.`config_name` = 'ppkbb_postscr_fields'");

$dt=time();
$sql="SELECT af.*, p.poster_id FROM `{$table_prefix}tracker_af` af LEFT JOIN `{$table_prefix}posts` p ON (af.post_msg_id=p.post_id) WHERE i_poster!='0'";
$result=my_sql_query($sql);
while($row=mysql_fetch_array($result))
{
	if(preg_match('/^thumb_/', $row['field_name']))
	{
		continue;
	}
	my_sql_query("INSERT INTO `{$table_prefix}attachments` (post_msg_id, topic_id, poster_id, is_orphan, real_filename, filetime, i_width, i_height, i_poster, i_external, attach_comment, extension, mimetype) VALUES('{$row['post_msg_id']}', '{$row['topic_id']}', '{$row['poster_id']}', '0', '{$row['field_value']}', '{$dt}', '{$row['i_width']}', '{$row['i_height']}', '{$row['i_poster']}', '1', '', 'jpg', 'image/jpeg')", true);
}
mysql_free_result($result);

my_sql_query("DELETE FROM `{$table_prefix}tracker_af` WHERE i_poster!='0'");

$result=my_sql_query("ALTER TABLE `{$table_prefix}tracker_af`
  DROP `i_width`,
  DROP `i_height`,
  DROP `f_size`,
  DROP `i_poster`");

$result=my_sql_query("INSERT INTO `{$table_prefix}tracker_config` (`config_name`, `config_value`) VALUES ('ppkbb_max_extposters', '')");
$result=my_sql_query("INSERT INTO `{$table_prefix}tracker_config` (`config_name`, `config_value`) VALUES ('ppkbb_max_extscreenshots', '')");

$result=my_sql_query("ALTER TABLE `{$table_prefix}topics` DROP `topic_addfields_poster`");

$result=my_sql_query("ALTER TABLE `{$table_prefix}users` ADD `user_uploaded_self` BIGINT( 20 ) UNSIGNED NOT NULL DEFAULT '0'");

$sql="SELECT SUM(s.uploaded), s.userid FROM {$table_prefix}tracker_snatched s, {$table_prefix}tracker_torrents t WHERE s.userid=t.poster_id AND s.torrent=t.id GROUP BY s.userid";
$result=my_sql_query($sql);
while($row=mysql_fetch_row($result))
{
	my_sql_query("UPDATE {$table_prefix}users SET user_uploaded_self='{$row[0]}' WHERE user_id='{$row[1]}'", true);
}
mysql_free_result();

mysql_close();
exit();

function my_sql_query($query, $silent=0)
{
	$result=@mysql_query($query);
	if(!$silent)
	{
		echo ($result ? '<b>Ok</b>: ' : '<span style="color:#FF0000"><b>Error</b>: ').$query.($result ? '' : '</span><br />['.mysql_errno().'] ('.mysql_error().')').'<br /><br />';
	}

	return $result;
}

function err($str)
{
	echo $str;
	mysql_close();
	exit();
}

?>
